//
//  ViewController.m
//  HorizontalTableView
//
//  Created by xueshan1 on 2019/7/18.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "ViewController.h"
#import "UIView+Externsion.h"
#import "SFHorizontalOuterTableView.h"
#import "SFHorizontalInnerTableView.h"
#import "CustomSegmentControl.h"

static const CGFloat OuterTableSectionHeight = 40;
#define SFScreenWidth [[UIScreen mainScreen] bounds].size.width
#define SFScreenHeight [[UIScreen mainScreen] bounds].size.height

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,assign)BOOL canScroll;
@property(nonatomic,strong)SFHorizontalOuterTableView *outTableView;
@property(nonatomic,strong)UIScrollView *contentScrollView;
@property(nonatomic,strong)NSMutableArray *itemViews;
@property(nonatomic,strong)SFHorizontalInnerTableView *currentItemView;
@property (nonatomic, strong) CustomSegmentControl * segmentBar;

@property(nonatomic,assign)NSInteger horizontalSection;// 需要水平滑动的第几个section
@property(nonatomic,assign)BOOL ignoredHeight; //一般如navBar tabBar的高度
/**
 作为tableView的组头 sectionHeaderView
 */
@property (strong, nonatomic) UIView *tableSectionHeaderView;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.s
    _horizontalSection = 0;
    _canScroll = YES;
    _ignoredHeight = 0;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeOuterTableViewScrollStatus) name:InnerTableViewLeaveTopNotification object:nil];

    
    [self.view addSubview:self.outTableView];
    self.outTableView.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    
    
}
- (SFHorizontalOuterTableView *)outTableView{
    if (!_outTableView) {
        _outTableView = [[SFHorizontalOuterTableView alloc]initWithFrame:self.view.bounds];
        _outTableView.showsVerticalScrollIndicator = NO;
        _outTableView.tableFooterView = [UIView new];
        _outTableView.delegate = self;
        _outTableView.dataSource = self;
        
        UIView *headerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SFScreenWidth, 200)];
        headerV.backgroundColor = [UIColor greenColor];
        _outTableView.tableHeaderView = headerV;
        
    }
    return _outTableView;
}
- (CustomSegmentControl * )segmentBar {
    if (nil == _segmentBar) {
        self.segmentBar = [[CustomSegmentControl alloc]initWithItems:@[@"Item0",@"Item1",@"Item2"]];
        _segmentBar.size = CGSizeMake(SFScreenWidth, 40);
        _segmentBar.font = [UIFont systemFontOfSize:15];
        _segmentBar.textColor = RGBColor(100, 100, 100);
        _segmentBar.selectedTextColor = RGBColor(0, 0, 0);
        _segmentBar.backgroundColor = RGBColor(249, 251, 198);
        _segmentBar.selectionIndicatorColor = RGBColor(249, 104, 92);
        _segmentBar.selectedSegmentIndex = 0;
        [_segmentBar addTarget:self action:@selector(changeSwipeViewIndex:) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentBar;
}
//tab bar ->改变 scrollView
- (void)changeSwipeViewIndex:(UISegmentedControl *)seg {
    [self.contentScrollView setContentOffset:CGPointMake(self.contentScrollView.width *seg.selectedSegmentIndex, 0) animated:NO];
    
}

- (void)changeOuterTableViewScrollStatus{
    self.canScroll = YES;
    //leaveTop后,每个tableview偏移量恢复
    for (UITableView *tb in self.itemViews) {
        [tb setContentOffset:CGPointZero];
    }
 
}
- (void)setCanScroll:(BOOL)canScroll{
    _canScroll = canScroll;
    for (SFHorizontalInnerTableView *tb in self.itemViews) {
        [tb setTabCanScroll:!canScroll];
    }
    
}
#pragma mark - *
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.contentScrollView) {
        CGFloat offsetX = scrollView.contentOffset.x;
        NSInteger index = offsetX / scrollView.width;
        self.segmentBar.selectedSegmentIndex = index;
        
        self.currentItemView = self.itemViews[index];
        
    }
    
    if (scrollView == self.outTableView) {
        CGFloat contentOffsetY = scrollView.contentOffset.y;
        
        if (contentOffsetY == 0) {
            [self changeOuterTableViewScrollStatus];
        }
        CGFloat tableViewSectionY = [self.outTableView rectForSection:_horizontalSection].origin.y;
        if (contentOffsetY >= tableViewSectionY) {//偏移量超过headerView时候, 则偏移量一直固定为tableViewSectionY, 来达到不能滚动
            scrollView.contentOffset = CGPointMake(0, tableViewSectionY);
            
            self.canScroll = NO;
            [self.currentItemView setTabCanScroll:YES];
        }else{
            if (!self.canScroll) {
                scrollView.contentOffset = CGPointMake(0, tableViewSectionY);
            }
        }
        NSLog(@"外层%f ; tbtableViewSectionY:%f",contentOffsetY, tableViewSectionY);
    }
    
    
}

#pragma mark - outer delegate
- (CGFloat)horizontalSectionCellHeight{
    CGFloat hei = SFScreenHeight - _ignoredHeight - OuterTableSectionHeight ;
    return hei;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  [self horizontalSectionCellHeight];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FixCellId"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FixCellId"];
        self.contentScrollView.height = [self horizontalSectionCellHeight];
        [cell.contentView addSubview:self.contentScrollView];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return OuterTableSectionHeight;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.segmentBar;
}



- (UIScrollView *)contentScrollView{
    if (!_contentScrollView) {
        _contentScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
        _contentScrollView.backgroundColor = [UIColor clearColor];
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.pagingEnabled = YES;
        _contentScrollView.delegate = self;
        _contentScrollView.contentSize = CGSizeMake(SFScreenWidth * 3, 0);
        
        for (int i = 0; i < 3; i++ ) {
            SFHorizontalInnerTableView *tb = [[SFHorizontalInnerTableView alloc]initWithFrame:CGRectMake(i * _contentScrollView.width, 0, _contentScrollView.width, _contentScrollView.height)];
            tb.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
            [_contentScrollView addSubview:tb];
            tb.tag = i;
            if (_itemViews == nil) {
                _itemViews = [NSMutableArray array];
            }
            [_itemViews addObject:tb];
            if (i == 0) {
                self.currentItemView = tb;
            }
        }
        
    }
    return _contentScrollView;
}







@end
